﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Game.Entity.Accounts;
using Game.Facade;
using Game.Kernel;
using Game.Utils;
using Newtonsoft.Json.Linq;
using ZXing;
using ZXing.QrCode;

namespace Game.Web.promote.common
{
    public class Agent 
    {
    
        /// <summary>
        /// 生成二维码,保存成图片
        /// </summary>
        public static  void Generate1(string gameId)
        {

            //在文件名前面加上时间，以防重名
            string imgName = gameId + "a.jpg";
            //文件存储相对于当前应用目录的虚拟目录
            string path = "/image/";
            string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通过此对象获取文件名
            if (Directory.Exists(imgPath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(imgPath );
            }


            string filename = imgPath + imgName;
      

            if (!System.IO.File.Exists(filename))
            {
                BarcodeWriter writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                QrCodeEncodingOptions options = new QrCodeEncodingOptions();
                options.DisableECI = true;
                //设置内容编码
                options.CharacterSet = "UTF-8";
                //设置二维码的宽度和高度
                options.Width = 430;
                options.Height = 430;
                //设置二维码的边距,单位不是固定像素
                options.Margin = 1;
                writer.Options = options;

                Bitmap map = writer.Write("http://47.106.163.85/downloadpage/download.html?gameId="+gameId);

          

                map.Save(filename, ImageFormat.Png);
                map.Dispose();
            }

            string newpath = imgPath + gameId + ".jpg";
            if (System.IO.File.Exists(newpath)) {
                return;
            }

             //嵌套图片
             string imgPathA = System.AppDomain.CurrentDomain.BaseDirectory + "/agent/images/";     //通过此对象获取文件名


            System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(imgPathA + "aa.png");

            System.Drawing.Image imgWarter = System.Drawing.Image.FromFile(filename); ;
      

            using (Graphics g = Graphics.FromImage(imgSrc))
            {

               // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
               // g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                g.DrawImage(imgWarter, new Rectangle(260,
                                                 1045,
                                                 500,
                                                 500),
                        0, 0, imgWarter.Width, imgWarter.Height, GraphicsUnit.Pixel);
            }

          

           // imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Png);

            Bitmap bmp = new Bitmap(imgSrc);
    
            bmp.Save(newpath, System.Drawing.Imaging.ImageFormat.Png);

        }

        private static System.Drawing.Image CutEllipse(System.Drawing.Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
             //   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
              //  g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            return bitmap;
        }
    }
}
